package com.yuanluludev.lib_base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import com.yuanluludev.lib_base.R;
/**
 * 编写人: yuanlulu
 * 功能描述: 通用TitleBar
 */
public class TitleBarView extends FrameLayout {

    private int leftImgSrc;
    private String titleText;
    private String rightText;
    private String leftText;
    private int rightImgSrc;
    private int leftImgVisible;
    private int rightTextVisible;
    private int leftTextVisible;
    private int rightImgVisible;

    private ImageView titleLeftBtn;
    private ImageView titleRightBtn;
    private TextView titleLeftText;
    private TextView titleRightText;
    private TextView title;
    RelativeLayout rlBg;

    public TitleBarView(Context context) {
        super(context);
        initView();
    }

    public TitleBarView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.TitleBarStyle);
    }

    public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView, defStyleAttr, R.style.DefaultTitleBarStyle);
        try {
            leftImgSrc = typedArray.getResourceId(R.styleable.TitleBarView_left_img_src, R.drawable.ic_baseline_arrow_back_24_white);
            titleText = typedArray.getString(R.styleable.TitleBarView_title_text);
            rightText = typedArray.getString(R.styleable.TitleBarView_right_text);
            leftText = typedArray.getString(R.styleable.TitleBarView_left_text);
            rightImgSrc = typedArray.getResourceId(R.styleable.TitleBarView_right_img_src, R.drawable.ic_baseline_arrow_back_24_white);
            leftImgVisible = typedArray.getInt(R.styleable.TitleBarView_left_img_visible, VISIBLE);
            rightTextVisible = typedArray.getInt(R.styleable.TitleBarView_right_text_visible, GONE);
            leftTextVisible = typedArray.getInt(R.styleable.TitleBarView_left_text_visible, VISIBLE);
            rightImgVisible = typedArray.getInt(R.styleable.TitleBarView_right_img_visible, GONE);
        } finally {
            typedArray.recycle();
        }
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.base_title_bar, null);
        title = (TextView) view.findViewById(R.id.title);
        titleLeftText = (TextView) view.findViewById(R.id.title_left_txt);
        titleLeftBtn = (ImageView) view.findViewById(R.id.title_left_btn);
        titleRightBtn = (ImageView) view.findViewById(R.id.title_right_btn);
        titleRightText = (TextView) view.findViewById(R.id.title_right_txt);
        rlBg = (RelativeLayout) view.findViewById(R.id.rl_bg);

        title.setText(titleText);
        titleLeftBtn.setVisibility(leftImgVisible);
        titleLeftBtn.setImageResource(leftImgSrc);
        titleRightBtn.setVisibility(rightImgVisible);
        titleRightBtn.setImageResource(rightImgSrc);
        titleRightText.setText(rightText);
        titleRightText.setVisibility(rightTextVisible);
        titleLeftText.setText(leftText);
        titleLeftText.setVisibility(leftTextVisible);
        this.addView(view);
    }


    public void setTitleText(CharSequence titleStr) {
        title.setText(titleStr);
    }

    public void setTitleTextClickListener(@NonNull OnClickListener listener) {
        title.setOnClickListener(listener);
    }

    public void setTitleRightDrawable(Drawable rightDrawable) {
        title.setCompoundDrawables(null, null, rightDrawable, null);
    }

    public void setTitleText(@StringRes int titleRes) {
        title.setText(titleRes);
    }

    public String getTitleText() {
        return title.getText().toString();
    }

    public TextView getTitleTextView() {
        return title;
    }

    public void setTitleColor(@ColorRes int colorRes) {
        title.setTextColor(getResources().getColor(colorRes));
    }

    public void setTextSize(int sizeSp) {
        title.setTextSize(TypedValue.COMPLEX_UNIT_SP, sizeSp);
    }

    public void setLeftBtnVisibility(int visibility) {
        titleLeftBtn.setVisibility(visibility);
    }

    public void setLeftBtnSrc(@DrawableRes int srcRes) {
        titleLeftBtn.setImageResource(srcRes);
    }

    public void setRightBtnVisibility(int visibility) {
        titleRightBtn.setVisibility(visibility);
    }

    public void setRightBtnSrc(@DrawableRes int srcRes) {
        titleRightBtn.setImageResource(srcRes);
    }

    public void setTitleRightText(Character titleStr) {
        titleRightText.setText(titleStr);
    }

    public void setTitleLeftText(Character titleStr) {
        titleLeftText.setText(titleStr);
    }

    public void setTitleRightText(@StringRes int titleRes) {
        titleRightText.setText(titleRes);
    }

    public void setTitleRightText(String titleText) {
        titleRightText.setText(titleText);
    }

    public void setTitleLeftText(@StringRes int titleRes) {
        titleLeftText.setText(titleRes);
    }

    public String getTitleRightText() {
        return titleRightText.getText().toString();
    }

    public String getTitleLeftText() {
        return titleLeftText.getText().toString();
    }

    public void setTitleRightColor(@ColorRes int colorRes) {
        titleRightText.setTextColor(getResources().getColor(colorRes));
    }

    public void setTitleLeftColor(@ColorRes int colorRes) {
        titleLeftText.setTextColor(getResources().getColor(colorRes));
    }

    public void setTextRightSize(int sizeSp) {
        titleRightText.setTextSize(TypedValue.COMPLEX_UNIT_SP, sizeSp);
    }

    public void setTextLeftSize(int sizeSp) {
        titleLeftText.setTextSize(TypedValue.COMPLEX_UNIT_SP, sizeSp);
    }

    public void setRightTextVisibility(int visibility) {
        titleRightText.setVisibility(visibility);
    }

    public void setLeftTextVisibility(int visibility) {
        titleLeftText.setVisibility(visibility);
    }

    public void setOnLeftBtnClickListener(@NonNull OnClickListener listener) {
        titleLeftBtn.setOnClickListener(listener);
    }

    public void setOnRightBtnClickListener(@NonNull OnClickListener listener) {
        titleRightBtn.setOnClickListener(listener);
    }

    public void setOnRightTextClickListener(@NonNull OnClickListener listener) {
        titleRightText.setOnClickListener(listener);
    }

    public void setOnLeftTextClickListener(@NonNull OnClickListener listener) {
        titleLeftText.setOnClickListener(listener);
    }

//    public void setTitleBarBackground(Drawable drawable) {
//        if (drawable != null) {
//            rlBg.setBackground(drawable);
//        }
//    }

    public void setTitleBarBackground(int color) {
        if (rlBg != null) {
            rlBg.setBackgroundColor(color);
        }
    }

}
